package com.groups.action;

import java.sql.Timestamp;
import java.util.Date;

import com.groups.entity.User;
import com.groups.service.UserService;


public class UserAction extends BaseAction{
	private UserService userService;
	private User user;
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public String toLogin() throws Exception{
		return "toLogin";
	}
	
	public String toReg() throws Exception{
		return "toReg";
	}
	public String doReg() throws Exception{
		Date regTime =  new Date();
		user.setPower(1);
		user.setUserTime(regTime);
		boolean flag = false;;
		flag = userService.addUser(user);
		if(!flag){
			return "toReg";
		}else{
			response.sendRedirect("Updategood!toList.action");
			return null;
		}
	}
	public String doLogin() throws Exception{
		String username = user.getUserName();
		String pwd = user.getUserPwd();
		int power = user.getPower();
		user = userService.checkUser(username, pwd,power);
		if(user!=null){
			if(power == 1){
				session.put("USER", user);
				response.sendRedirect("Updategood!toList.action");
			}else{
				session.put("USER", user);
				response.sendRedirect("Admin.jsp");
			}
			
			return null;
		}else{
			return "toLogin";
		}
	}
	public String doLogout() throws Exception{
		request.getSession().invalidate();
		return "toLogin";
	}
}

